package com.example.app;

import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    public void onClick (View view){

        TextView snt = (TextView)
                view.findViewById(R.id.shape_name_text);

        String Triangle = snt.getText().toString();
        String triangle = getResources().getString(R.string.triangle);
        if (Triangle.equals(triangle)) {
            Intent triangle2 = new Intent(this, TriangleActivity.class);
            startActivity(triangle2);

        String Circle = snt.getText().toString();
        String circle = getResources().getString(R.string.triangle);
        if (Circle.equals(circle)) {
            Intent circle2 = new Intent(this, CircleActivity.class);
            startActivity(circle2);

        String Square = snt.getText().toString();
        String square = getResources().getString(R.string.triangle);
            if (Square.equals(square)) {
                Intent square2 = new Intent(this, SquareActivity.class);
                startActivity(square2);
        }
        }
        }
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            ViewGroup shapeList =
                    (ViewGroup)rootView.findViewById(R.id.shapes);

            Resources res = getResources();
            TypedArray shape_names = res.obtainTypedArray(R.array.shape_names);
            TypedArray shape_icons = res.obtainTypedArray(R.array.shape_icons);

            for (int n = 0; n < shape_names.length(); n = n + 1) {
                View shape_view = inflater.inflate(R.layout.shape, null);
                TextView shape_name_text =
                       (TextView)shape_view.findViewById(R.id.shape_name_text);

                ImageView shape_icon_image =
                        (ImageView)shape_view.findViewById(R.id.shape_icon_image);

                shape_name_text.setText( shape_names.getText(n) );
                shape_icon_image.setImageDrawable( shape_icons.getDrawable(n) );
                shapeList.addView(shape_view);
            }


            return rootView;
        }
    }

}
